async function startStreamChat() {
    const fileInput = document.getElementById('image-upload');
    const button = document.getElementById('upload-btn');
    const resultContainer = document.getElementById('result-container');

    // 检查是否选择了文件
    if (!fileInput.files || !fileInput.files[0]) {
        alert('请先选择图片');
        return;
    }

    // 添加加载状态
    button.classList.add('loading');
    button.disabled = true;
    resultContainer.innerHTML = '<p>正在识别中...</p>';


    try {
        const formData = new FormData();
        formData.append('file', fileInput.files[0]);
        formData.append('prompt', fileInput.value);


        const response = await fetch('/api/chat-stream', {
            method: 'POST',
            body: formData
        });
         if (!response.ok) {
            throw new Error('识别请求失败');
        }
        resultContainer.innerHTML = '';

        const reader = response.body.getReader();
        const decoder = new TextDecoder();


        while (true) {
            const {value, done} = await reader.read();
            if (done) break;

            const chunk = decoder.decode(value);
            const lines = chunk.split('\n');

            for (const line of lines) {
                if (line.startsWith('data: ')) {
                    const data = JSON.parse(line.slice(6));
                    resultContainer.innerHTML += data.content;
                }
            }
        }
    } catch (error) {
        console.error('错误:', error);
        resultContainer.innerHTML = '<p class="error">对话过程中发生错误</p>';
    } finally {
        // 恢复按钮状态
        button.classList.remove('loading');
        button.disabled = false;
    }
}